<?php

class SitemenuAction extends fvAction {
    
    function __construct () {
        parent::__construct(fvSite::$Layoult);
    }

    function executeIndex() {
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_NO_LAYOULT;
        else return self::$FV_OK;
    }
    
    function executeEdit() {
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }
    
    function executeUp() {
    	$request = fvRequest::getInstance();
    	if (!$Sitemenu = SitemenuManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
    		fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
    	}
    	if (!$Sitemenu2 = SitemenuManager::getInstance()->getAll("menu_order < ".$Sitemenu->menu_order, "menu_order desc", null)){
    		fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
    	}
    	$Sitemenu2 =$Sitemenu2[0];
    	$tmp = $Sitemenu->menu_order ;
    	$Sitemenu->menu_order = $Sitemenu2->menu_order;
    	$Sitemenu2->menu_order = $tmp;
    	$Sitemenu->save("false");
    	$Sitemenu2->save("false");
    	fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
    	if (fvRequest::getInstance()->isXmlHttpRequest())
    	return self::$FV_AJAX_CALL;
    	else return self::$FV_OK;
    }
    
    function executeDown() {
    	$request = fvRequest::getInstance();
    	if (!$Sitemenu = SitemenuManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
    		fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
    	}
    	if (!$Sitemenu2 = SitemenuManager::getInstance()->getAll("menu_order > ".$Sitemenu->menu_order, "menu_order asc", null)){
    		fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
    	}
    	$Sitemenu2 =$Sitemenu2[0];
    	$tmp = $Sitemenu->menu_order ;
    	$Sitemenu->menu_order = $Sitemenu2->menu_order;
    	$Sitemenu2->menu_order = $tmp;
    	$Sitemenu->save("false");
    	$Sitemenu2->save("false");
    	fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
    	if (fvRequest::getInstance()->isXmlHttpRequest())
    	return self::$FV_AJAX_CALL;
    	else return self::$FV_OK;
    }
    
    function executeSave() {
        $request = fvRequest::getInstance();
        if (!$Sitemenu = SitemenuManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $Sitemenu = new Sitemenu();
        }
        $n = $request->getRequestParameter('n');
        $n["lang"] = fvSite::$fvSession->get("lang");
        if ($Sitemenu->isNew()) {
        	$n['menu_order'] = SitemenuManager::getInstance()->getMax()+1;
        }
        $Sitemenu->updateFromRequest($n);
        if ($Sitemenu->save()) {
            fvResponce::getInstance()->setHeader('Id', $Sitemenu->getPk());
            $this->setFlash("Данные успешно сохранены", self::$FLASH_SUCCESS);
        } else { 
            fvResponce::getInstance()->setHeader('X-JSON', json_encode($Sitemenu->getValidationResult()));
            $this->setFlash("Ошибка при сохранении данных проверьте правильность введенных данных", self::$FLASH_ERROR);
        }
        if ($request->getRequestParameter('redirect')) {
            fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
        }        
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }    

    function executeDelete() {
        $request = fvRequest::getInstance();
        if (!$Sitemenu = SitemenuManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $this->setFlash("Ошибка при удалении.", self::$FLASH_ERROR);
        } else {
            $Sitemenu->delete();
            $this->setFlash("Данные успешно удалены", self::$FLASH_SUCCESS);
        }
        
        fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_NO_LAYOULT;
        else return self::$FV_OK;
    }

}

?>
